﻿using MahApps.Metro.Controls;
using MaterialDesignThemes.Wpf;
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using UpperComputer.WPF.Entry.Common.Extensions;

namespace UpperComputer.WPF.Entry.Common
{
    /// <summary>
    /// ConfirmDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ConfirmDialog : MetroWindow
    {
        private string _text;

        private double _msgOriginWidth;

        private double _msgOriginHeight;

        public ConfirmDialog(string text, string caption, MessageBoxImage icon, bool isShowCancel)
        {
            InitializeComponent();
            _text = text;
            Title = caption;
            TitleLabel.Content = caption;
            switch (icon)
            {
                case MessageBoxImage.Information:
                default:
                    MessageIcon.Kind = PackIconKind.Information;
                    break;
                case MessageBoxImage.Warning:
                    MessageIcon.Kind = PackIconKind.Warning;
                    MessageIcon.Foreground = new SolidColorBrush(Colors.Yellow);
                    break;
                case MessageBoxImage.Error:
                    MessageIcon.Kind = PackIconKind.Error;
                    MessageIcon.Foreground = new SolidColorBrush(Colors.Red);
                    break;
                case MessageBoxImage.Question:
                    MessageIcon.Kind = PackIconKind.HelpCircle;
                    break;
            }
            if (isShowCancel)
            {
                CancelBtn.Visibility = Visibility.Visible;
            }
            SourceInitialized += OnSourceInitialized;
            Loaded += ConfirmDialog_Loaded;
        }

        private void ConfirmDialog_Loaded(object sender, RoutedEventArgs e)
        {
            double showWidth = MessagePanel.ActualWidth - MessageIcon.ActualWidth - 10;
            _msgOriginWidth = showWidth > MessageLabel.ActualWidth ? showWidth : MessagePanel.DesiredSize.Width - MessageIcon.ActualWidth - 60;
            _msgOriginHeight = MessageIcon.ActualHeight;
            MessageLabel.SizeChanged += MessageLabel_SizeChanged;
            MessageLabel.Text = _text;
        }

        private void OnSourceInitialized(object sender, EventArgs e)
        {
            IntPtr handle = new WindowInteropHelper(this).Handle;
            IntPtr style = GetWindowLong(handle, GWL_EXSTYLE);
            SetWindowLong(handle, GWL_EXSTYLE, new IntPtr(style.ToInt32() | WS_EX_NOACTIVATE));
        }

        private void MessageLabel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double width = Width - _msgOriginWidth + MessageLabel.ActualWidth;
            double height = Height - _msgOriginHeight + MessageLabel.ActualHeight;
            if (width > Width)
            {
                Width = width;
            }
            if (height > Height)
            {
                Height = height;
            }
        }

        private void Border_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.OnMouseDownMethod(e);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        #region 本地函数
        private const int WS_EX_TOPMOST = 0x00000008;
        private const int WS_EX_NOACTIVATE = 0x08000000;
        private const int GWL_EXSTYLE = -20;
        private static IntPtr GetWindowLong(IntPtr hWnd, int nIndex)
        {
            return Environment.Is64BitProcess ? GetWindowLong64(hWnd, nIndex) : GetWindowLong32(hWnd, nIndex);
        }

        private static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
        {
            return Environment.Is64BitProcess ? SetWindowLong64(hWnd, nIndex, dwNewLong) : SetWindowLong32(hWnd, nIndex, dwNewLong);
        }

        [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
        public static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
        public static extern IntPtr GetWindowLong64(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
        public static extern IntPtr SetWindowLong32(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
        public static extern IntPtr SetWindowLong64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        #endregion
    }
}
